package apig.server;

import java.io.*;

import org.h2.jdbcx.*;

import apii.utils.*;

/**
 * <b>Note:</b> Thread SAFE.
 * <p>
 * <b>Note:</b> Doesn't create threads.
 * 
 * @author Alex
 * 
 */
public class ServerMain {

	public static void main(String[] args) {
		
		new Thread() {
			@Override
			public void run() {
				runThread();
			}
		}.start();
	}

	public void start() {
	}

	public void finish() {
		ServerThings.getThings().finish();
	}
	
	
	
	
	public static final void runThread() {
		String tim = System.currentTimeMillis() + "-"
				+ (int) (Math.random() * 1000000);
		try {
			File logs = new File("logs");
			logs.mkdirs();
			if (!logs.isDirectory()) {
				throw new FileNotFoundException(
						"Can not create logs directory: "
								+ logs.getPath());
			}
			System.setOut(new PrintStream(new DoubledStream(System.out,
					new FileOutputStream(new File(logs, "out" + tim
							+ ".txt"))), true));
			System.setErr(new PrintStream(new DoubledStream(System.err,
					new FileOutputStream(new File(logs, "err" + tim
							+ ".txt"))), true));
			new ServerMain().start();
			new ServerMain().finish();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
}
